import { app, shell, Menu, BrowserWindow } from 'electron'

const menus = (win) => {
  return [
    {
      label: ' 操作 ',
      role: 'option',
      submenu: [
        {
          label: '主页',
          click: () => {
            win.webContents.send('back-index');
          }
        },
        {
          label: '重载',
          accelerator: 'CmdOrCtrl+R',
          click: (item, focusedWindow) => {
            if (focusedWindow) {
              // 重载之后, 刷新并关闭所有的次要窗体
              if (focusedWindow.id === 1) {
                BrowserWindow.getAllWindows()
                  .forEach((win) => {
                    if (win.id > 1) {
                      win.close()
                    }
                  })
              }
              focusedWindow.reload()
            }
          }
        }, {
          label: '切换全屏',
          accelerator: process.platform === 'darwin' ? 'Ctrl+Command+F' : 'F11',
          click: (item, focusedWindow) => {
            if (focusedWindow) {
              focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
            }
          }
        }, {
          label: '切换开发者工具',
          accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
          click: (item, focusedWindow) => {
            if (focusedWindow) {
              focusedWindow.toggleDevTools()
            }
          }
        }
      ]
    }, {
      label: '窗口',
      role: 'window',
      submenu: [{
        label: '最小化',
        accelerator: 'CmdOrCtrl+M',
        role: 'minimize'
      }, {
        label: '关闭',
        accelerator: 'CmdOrCtrl+W',
        role: 'close'
      }, {
        type: 'separator'
      }, {
        label: '重新打开窗口',
        accelerator: 'CmdOrCtrl+Shift+T',
        key: 'reopenMenuItem',
        click: () => {
          app.emit('activate')
        }
      }]
    }, {
      label: '关于',
      role: 'about',
      submenu: [{
        label: '学习更多',
        click: () => {
          shell.openExternal('http://electron.atom.io')
        }
      }]
    }
  ];
};

export { menus };
